﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace WindowsFormsListViewLabelEdit
{
    public partial class Form1 : Form
    {
        // 当前鼠标事件点中的项
        private ListViewItem mouseClickItem;
        public Form1()
        {
            InitializeComponent();

            InitListView();

            // 添加几项数据
            AddListItem(new Student(20201001, "shao", "13810012345"));
            AddListItem(new Student(20201002, "wang", "18799122300"));
            AddListItem(new Student(20201003, "li", "13490912322"));
        }

        private void InitListView()
        {
            // 设置显示模式
            listView1.View = View.Details;
            // 整行选中
            // listView1.FullRowSelect = true;

            listView1.LabelEdit = true;

            // 设置列名
            // 宽度值-2表示自动调整宽度
            listView1.Columns.Add("姓名", 150, HorizontalAlignment.Left);
            listView1.Columns.Add("学号", 150, HorizontalAlignment.Left);
            listView1.Columns.Add("手机号", -2, HorizontalAlignment.Left);

        }

        private void AddListItem(Student stu)
        {
            ListViewItem item = new ListViewItem(stu.Name, 0);
            item.Tag = stu;

            item.SubItems.Add(stu.Id + "");
            item.SubItems.Add(stu.Phone);
            listView1.Items.Add(item);
        }

        // 鼠标右键点击，弹出菜单
        private void listView1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ListViewItem item = listView1.GetItemAt(e.X, e.Y);
                this.mouseClickItem = item;

                menu_Rename.Enabled = (item != null);

                contextMenuStrip1.Show(listView1, e.Location);
            }
        }

        // 点击 '重命名' 菜单
        private void menu_Rename_Click(object sender, EventArgs e)
        {
            this.mouseClickItem.BeginEdit();
        }



        // 开启列表项的编辑
        private void listView1_BeforeLabelEdit(object sender, LabelEditEventArgs e)
        {

        }
        // 结束列表项的编辑
        private void listView1_AfterLabelEdit(object sender, LabelEditEventArgs e)
        {
            int index = e.Item; // 当前项的索引
            string label = e.Label; // 新输入的文本

            // 检查是否冲突
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                if (index == i) continue;
                if (label == listView1.Items[i].Text)
                {
                    e.CancelEdit = true;
                    MessageBox.Show("名字重复", "提示");
                    return;
                }
            }

            e.CancelEdit = false; // 接受此输入

            // 更新后台数据
            Student stu = (Student)listView1.Items[index].Tag;
            stu.Name = label;
        }
    }
}
